<?php

/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Review & Rating Synthesis
 * @version 	1.0.2
 * @author 		Rukan <rukanhitokiri@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://www.zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

require_once (dirname(__FILE__).'/../classes/ZProductReview.php');

require_once (_PS_CLASS_DIR_.'Customer.php');

require_once (_PS_CLASS_DIR_.'FrontController.php');
include_once(dirname(__FILE__).'/../classes/ZCustomerBadges.php');

class ZreviewController extends FrontControllerCore {

    public function process() {
    	global $cookie;
        parent::process();
        if ($id_customer = (int)Tools::getValue('id_customer')){
        	
        	$customerBadges = ZCustomerBadges::getCustomerByBadges((int)$cookie->id_lang);
        	$arrCustomer = array();
        	$badges = '';
        	foreach ($customerBadges AS $customer){
        		$arrCustomer = explode(',', $customer['id_zcustomer']);
				if(in_array($id_customer, $arrCustomer)){
					$badges .= ($badges =='' ? '' : '</span><span class="new">').$customer['name'];
				}
        	}
        	
        	
        	
            $nbReviews = (int)ZProductReview::getReviewNumberByCustomer($id_customer);
            $this->pagination($nbReviews);
            $reviews = ZProductReview::getReviewsByCustomer($id_customer, $this->p, $this->n ,$this->orderBy, $this->orderWay, (int)(self::$cookie->id_lang));
			$vlike = 0;
			$vdislike = 0;
            foreach ($reviews as $review){
				$vlike += $review['vlike'];
				$vdislike += $review['vdislike'];
			}
			$vdislike = $vdislike + $vlike;
            self::$smarty->assign(array('reviews' => $reviews,
            							'badges' => $badges,
            							'vlike' => $vlike,
            							'vdislike' => $vdislike,
            							'count' => ZProductReview::getReviewNumberByCustomer($id_customer)));

            $customer = new CustomerCore($id_customer);
            $customerFields = $customer->getFields();
            
            $sql = ' SELECT *
            	FROM `'._DB_PREFIX_.'zalias_custommers` zc
            	WHERE zc.`id_custommers` = '.(int)$id_customer;
            $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
            if($row AND $row['active'] == 2) {
            	self::$smarty->assign(array('customer_name' => $row['alias']));
            } else {
            	self::$smarty->assign(array('customer_name' => $customerFields['firstname'].' '.$customerFields['lastname']));
            }

            self::$smarty->assign('param_p', $this->p);
            self::$smarty->assign('param_n', $this->n);

        }
    }

    public function displayContent() {
        parent::displayContent();
        self::$smarty->assign(array(
    		'configRating' 			=> ZprofessConfigure::$configRating,
    		'enable_email' 			=> (int)Configuration::get('ZPRO_ENABLE_EMAIL'),
    		'enable_website' 		=> (int)Configuration::get('ZPRO_ENABLE_WEBSITE'),
    		'display_email' 		=> (int)Configuration::get('ZPRO_DISPLAY_EMAIL'),
    		'display_website' 		=> (int)Configuration::get('ZPRO_DISPLAY_WEBSITE'),
    		'display_title' 		=> (int)Configuration::get('ZPRO_DISPLAY_TITLE'),
    		'only_review' 			=> (int)Configuration::get('ZPRO_ONLY_REVIEW'),
    		'enable_vote' 			=> (int)Configuration::get('ZPRO_ENABLE_VOTE'),
                'sort_dir'          => dirname(__FILE__) . '/../themes/review_sort.tpl',
    		'pagination_dir'		=> _PS_THEME_DIR_ . 'pagination.tpl',
    	));
        self::$smarty->display(_PS_MODULE_DIR_ . 'reviewratingsynthesis/themes/allreview.tpl');
    }

    public function  displayHeader() {
        parent::displayHeader();
        $this->reviewSort();
    }

    public function reviewSort(){
        if (!self::$initialized)
            $this->init();

        $this->orderBy = Tools::getValue('orderby', 'date_add');
        $this->orderWay = Tools::getValue('orderway', 'desc');

        self::$smarty->assign(array(
                'orderby' => $this->orderBy,
                'orderway' => $this->orderWay,
                'orderbydefault' => 'date_add',
                'orderwaydefault' => 'desc',));
    }

}
?>
